import javax.naming.TimeLimitExceededException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JSplitPane;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Radar extends JFrame {
	public Signal emit;
	public Signal ret;
	public Signal korel;
	
	private double sspeed; // predkosc rozchodzenia sie
	private double rspeed;
	private double distance;
	
	private JTextField distance_txt;
	private SignalOperations so;
	private JTextField time_txt;

	public Radar(Signal s) {
		so = new SignalOperations();
		setTitle("Radar");
		setResizable(false);
		emit = s;
		distance = 0;
		sspeed = 20;
		rspeed = 10;

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JButton btnAnuluj = new JButton("Anuluj");
		btnAnuluj.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
			}
		});
		btnAnuluj.setBounds(166, 211, 63, 23);
		panel.add(btnAnuluj);

		JButton btnPolicz = new JButton("Policz");
		btnPolicz.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				antena();
			}
		});
		btnPolicz.setBounds(20, 211, 63, 23);
		panel.add(btnPolicz);

		distance_txt = new JTextField();
		distance_txt.setHorizontalAlignment(SwingConstants.CENTER);
		distance_txt.setText("0");
		distance_txt.setColumns(10);
		distance_txt.setBounds(152, 143, 53, 20);
		panel.add(distance_txt);

		JLabel label = new JLabel("Odleg\u0142o\u015B\u0107 odbiornika:");
		label.setBounds(20, 146, 125, 14);
		panel.add(label);

		JLabel lblCzasUpynity = new JLabel("Czas up\u0142yni\u0119ty :) ");
		lblCzasUpynity.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblCzasUpynity.setBounds(20, 44, 109, 14);
		panel.add(lblCzasUpynity);

		time_txt = new JTextField();
		time_txt.setText("0");
		time_txt.setHorizontalAlignment(SwingConstants.CENTER);
		time_txt.setColumns(10);
		time_txt.setBounds(152, 41, 53, 20);
		panel.add(time_txt);

		setBounds(50, 50, 252, 275);
		setVisible(true);
	}

	public void antena() {
		Signal korelacja;
		double t = Double.parseDouble(time_txt.getText());
		ret = reciever(emit, t);
		korelacja = so.korelacja(emit, ret);
		korel = korelacja;
		//korelacja = so.korelacja2(emit, ret);
		distance = computeDistance(sspeed, findFirstMax(korelacja));
		distance_txt.setText(new String("" + distance));
		dispose();
	}

	public void waiting(int n) {
		System.out.println(n);
		long t0, t1;
		t0 = System.currentTimeMillis();
		do {
			t1 = System.currentTimeMillis();
		} while ((t1 - t0) < (n * 1000));
	}

	private Signal reciever(Signal rec, double t) {
		Signal result;
		double dist = rspeed * t; // 20m\s
		//double timed = dist;
		//System.out.print("przesunalem o:"+dist);
		int ile = (int) (rec.getSamples() / rec.getF());
		double[] tab = new double[rec.getSamples()];
		int offset = (int) dist;
		for (int i = 0; i < tab.length; i++) {
			if (i < offset)
				tab[i] = rec.getContent()[tab.length-offset+i];
			if (i >= offset)
				tab[i] = rec.getContent()[i - offset];
		}
		
		result = new Signal(rec.getT0(), rec.getF(), rec.getSamples(), tab);
		return result;
	}

	private double findFirstMax(Signal korel) {
		korel.drawSignal();
		double time = 0; // time difference
		double max = 0;
		int maxi = 0;
		boolean foundFirst = false;
		double d = emit.getSamples()/emit.getF();
		double deltat = d / emit.getSamples();
		double[] kor = new double[korel.getSamples()];
		for (int i =0; i<kor.length; i++){
			kor[i] = korel.getContent()[i];
			System.out.println("i:"+i+">>"+kor[i]);
		}
		System.out.println("srodek korelacji: " + (kor.length / 2));
		for (int i = kor.length / 2; i < kor.length; i++) {
			if (kor[i]>max){
				max = kor[i];
				maxi =i;
			}
		}
		for (int i = kor.length / 2; i < kor.length; i++) {
			if(kor[i]==max){ 
				if(foundFirst==false){ maxi = i; foundFirst=true;}
			}
		}
		System.out.println("pierwsze max [i:" + maxi + "][max:" + max + "]");

		int ilediff = maxi - kor.length / 2;
		System.out.println("Odebralem przesuniety o: "+ilediff);
		time = (double)(ilediff) * deltat;
		// te okresy jakos
		// jak bedzie to 1 okres to t bedzie po prosu odlegloscia od srodka
		// jak bedzie wiecej okresow to bedzie trzeba wymyslec jak wziac tylko
		// pierwsze np: dodatkowy warunek czy rowne do tej pory max,
		// jezeli tak to domniemac ze to drugie maximum i brac tylko 1-sze
		// ale potem trzeba obliczyc jakos time z ile trwa okres jakos tak
		System.out.println("Opoznionny o [s]: "+time+" dla czasu 1 probki"+deltat);
		return time;
	}

	private double computeDistance(double speed, double time) {
		double distance;
		double s = 0;
		s = speed * time;
		distance = s / 2;
		System.out.println("Odleglosc="+distance+" s:"+speed*time+" v:"+speed+" t:"+time);
		return distance;
	}
}
